﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace odinshrine
{
    class Forces
    {
       public Vector2 force;
       public int time;

       public Forces(Vector2 force, int time)
       {
           this.time = time;
           this.force = force;
       }
    }

    class ForcesHandler
    {
        public int elapsedtime;
        private Vector2 _myspeed = Vector2.Zero;
        private Vector2 _myForce = Vector2.Zero;
        private  Dictionary<string,Forces> Overall = new Dictionary<string,Forces>();
        public Vector2 MyForce
        {
            get{ return _myForce;}
            set { 
                _myForce = value;
                _myspeed += value ;
                }
        }
        public Vector2 Myspeed
        {   
            get { return _myspeed; }
            set { _myspeed = value; }
        }
       

        public ForcesHandler()
        {
            Overall.Add("Gravity",new Forces(new Vector2(0, 3),0));
        }

        public void AddNew(string name,Vector2 addedone,int time)
        {
            if (!Overall.ContainsKey(name))
                Overall.Add(name, new Forces(addedone, time + elapsedtime));
            else
                Overall[name] = new Forces(addedone, time + elapsedtime);
        }

        public void Remove(string name)
        {
            if (Overall.ContainsKey(name))
            Overall.Remove(name);
        }
        private void Reaction(bool groundtouch)
        {
            if (groundtouch)
            {
                MyForce = new Vector2(MyForce.X,0);
                Myspeed = new Vector2(Myspeed.X, 0);
            }
        }

        private void Gather()
        {
            MyForce = Vector2.Zero;
            foreach (KeyValuePair<string,Forces> vect in Overall)
            {
                MyForce += vect.Value.force;
            }
        }

        private void KillTheDead()
        {
            List<string> removelist = new List<string>();
            foreach (KeyValuePair<string, Forces> vect in Overall)
            {

                if (vect.Value.time > 0 && vect.Value.time < elapsedtime)
                {
                    removelist.Add(vect.Key);
                }
            }
            foreach (string removed in removelist)
            {
                Overall.Remove(removed);
            }
        }

        public void Update(bool groundtouch, GameTime Elapsed)
        {
            elapsedtime += Elapsed.ElapsedGameTime.Milliseconds;
            KillTheDead();
            Gather();
            Reaction(groundtouch);
        }

    }
}
